package com.geh.learn.guava;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Immutable Collection Show Case
 *
 * @author 戈輝
 */
public class ShowImmutableCase {

    private static final AtomicReference<ImmutableMap<String, User>> userMapRef = new AtomicReference<>(ImmutableMap.of());

    static {
        init();
    }

    /**
     * user info init
     */
    public static void init() {
        // 下面的处理实际可能从数据库或者可变的配置文件中读取
        User user01 = new User("userKaki01", "東京");
        User user02 = new User("userKaki02", "大阪");
        Map<String, User> userMap = Maps.newHashMap();
        userMap.put(user01.getUsername(), user01);
        userMap.put(user02.getUsername(), user02);

        // 初始化可以对外访问的集合类型
        userMapRef.set(ImmutableMap.copyOf(userMap));
    }

    public static void main(String[] args) {
        System.out.println("userKaki01 exist ? : " + userMapRef.get().containsKey("user01"));
    }
}

class User {

    private String username;

    private String address;

    public User(String username, String address) {
        this.username = username;
        this.address = address;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
